import os
import subprocess
import datetime
import re
import gtk
from Deployer import Deployer
from BuildWizException import BuildWizException
from DlgShowMountedDevices import DlgShowMountedDevices

class PenDeployer( Deployer):
    def __init__( self, wizard, outputwin, platfname, info):
        Deployer.__init__( self, wizard, outputwin)
        self.__wiz = wizard
        self.__outputWin = outputwin
        self.__platfname = platfname
        self.__deployInfo = info
        self.__deployInfoReady = {}
        self.__penLocation = ()
        self.__getPendriveLocation()
        if len( self.__penLocation) > 0:
            # Cleaning dir is necessary because 'os.path.join()' will returns
            # the very second parameter if such one starts with '/' or './' or '../'
            cleanedDir = ""
            m = re.search( "^\.{0,2}\/(.*)", self.__deployInfo[ "destiny_dir"])
            if m:
                cleanedDir = m.group( 1) # ok, without dots and slash
            else:
                cleanedDir = self.__deployInfo[ "destiny_dir"]

            self.__deployInfoReady[ "fromDir"] = self.__deployInfo[ "source_dir"]
            self.__deployInfoReady[ "device"] = self.__penLocation[ 0]
            self.__deployInfoReady[ "mountpoint"] = self.__penLocation[ 1]
            dirtmp = os.path.join( self.__penLocation[ 1], cleanedDir)
            self.__deployInfoReady[ "toDir"] = os.path.join( dirtmp, self.__createDirName())
            self.__deployInfoReady[ "linkdirname"] = self.__deployInfo[ "link_2_dest_dir"]



    def __createDirName( self):
        now = datetime.datetime.now()
        dirname = ""
        if self.__deployInfo[ "dir_name_datetime"] == "YYYYMMDDHHMMSS":
            dirname = self.__deployInfo[ "dir_name"] + "-" + now.strftime("%Y%m%d%H%M%S")
        if self.__deployInfo[ "dir_name_datetime"] == "DDMMYYYYHHMMSS":
            dirname = self.__deployInfo[ "dir_name"] + "-" + now.strftime("%d%m%Y%H%M%S")
        
        return dirname

    def __getPendriveLocation( self):
        try:
            dlg = DlgShowMountedDevices( "Deploy: choose where pendrive is mounted", self.__wiz)
            response = dlg.run()
            if response == gtk.RESPONSE_OK: 
                self.__penLocation = dlg.getChoosenDevice()
            else:
                self.__penLocation = ()
        except BuildWizException, e:
            print "PenDeployer.getPendriveLocation(): has been failed!\n", e.getStrError()
            return
        
    def __confirmDeployment( self):
        if len( self.__penLocation) > 0:
            msg = "<span font_desc=\"monospace 9\">" \
                + "<u>Pendrive</u>:\n<b>" + self.__deployInfoReady[ "mountpoint"] + "</b>\n\n"\
                + "<u>Source directory</u>:\n<b>" + self.__deployInfo[ "source_dir"] + "</b>\n\n" \
                + "<u>Destiny directory</u>:\n<b>" + self.__deployInfoReady[ "toDir"] + "</b>\n\n" \
                + "<u>Link to destiny directory</u>:\n<b>" + self.__deployInfoReady[ "linkdirname"] + "</b></span>\n\n\n" \
                + "\tDo you want to proceed with deployment?"
            
            dlg = gtk.MessageDialog( None, 0
                                   , gtk.MESSAGE_QUESTION
                                   , gtk.BUTTONS_YES_NO)
            dlg.set_markup( msg)
            dlg.set_title( "Pendrive deployment information")
            response = dlg.run()
            dlg.destroy() 
            if response == gtk.RESPONSE_YES:
                return True

        return False

    def deploy( self):
        if self.__confirmDeployment():
            #self.__deployInfoReady[ "rootpassword"] = Deployer.getRootPassword( self)
            #if len( self.__deployInfoReady[ "rootpassword"]) > 0:
            #    return Deployer.deploy( self, self.__deployInfoReady)
            return Deployer.deploy( self, self.__deployInfoReady)

        return False

